/**************************************************/
//  WSGHttpTool.h
//  网络请求工具类,负责整个项目中所有的Http网络请求
/**************************************************/

#import "WSGHttpTool.h"


@implementation WSGHttpTool

/*
 * GET请求
 */
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    // 获取请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", nil];
    
    // 发送GET请求
    [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}

/*
 * POST请求
 */
+ (NSURLSessionDataTask *)post:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    // 获取请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 发送POST请求
    NSURLSessionDataTask *task = [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
    
    return task;
}

@end
